package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 * @author roche
 * @since 2024-12-11
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper  regionMapper;

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () -> baseMapper.selectServeListByRegionId(dto.getRegionId()));
    }

    @Override
    public void add(List<ServeUpsertReqDTO> list) {
        for (ServeUpsertReqDTO dto : list) {
            LambdaQueryWrapper<ServeItem> select = new LambdaQueryWrapper<ServeItem>()
                    .eq(ServeItem::getId, dto.getServeItemId())
                    .select(ServeItem::getId,ServeItem::getActiveStatus);
            ServeItem serveItem = serveItemMapper.selectOne(select);
            //1. 如果任意服务项未启动，不能添加区域服务
            if(serveItem == null || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("不能添加未启用的服务");
            }
            //2. 如果任意服务项已经在区域服务中，不能重复添加
            Integer count = lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }
            //3. 封装实体类对象，添加到数据库
            Serve serve = BeanUtil.toBean(dto, Serve.class);
            LambdaQueryWrapper<Region> cityCode = new LambdaQueryWrapper<Region>().eq(Region::getId, dto.getRegionId()).select(Region::getCityCode);
            String code = regionMapper.selectOne(cityCode).getCityCode();
            serve.setCityCode(code);
            save(serve);
        }
    }

    @Override
    public void updatePrice(Long id, BigDecimal price) {
        boolean updated = lambdaUpdate()
                .set(Serve::getPrice, price)
                .eq(Serve::getId, id)
                .update();
        if (!updated) {
            throw new CommonException("修改服务价格失败");
        }
    }

    @Override
    public void onSale(Long id) {
        //1. 查询区域服务
        Serve serve = lambdaQuery().eq(Serve::getId, id)
                .select(Serve::getSaleStatus,Serve::getServeItemId)
                .one();
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //2. 如果不是草稿或下架状态，则不允许上架
        Integer saleStatus = serve.getSaleStatus();
        if (FoundationStatusEnum.INIT.getStatus() != saleStatus
                && FoundationStatusEnum.DISABLE.getStatus() == saleStatus) {
            throw new ForbiddenOperationException("草稿或下架状态才允许上架");
        }
        LambdaQueryWrapper<ServeItem> serveItemLambdaQueryWrapper = new LambdaQueryWrapper<ServeItem>()
                .eq(ServeItem::getId, serve.getServeItemId())
                .select(ServeItem::getId, ServeItem::getActiveStatus);
        ServeItem serveItem = serveItemMapper.selectOne(serveItemLambdaQueryWrapper);
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项未启用，不允许上架");
        }

        //4. 上架区域服务，修改状态
        boolean updated = lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id)
                .update();
        if (!updated) {
            throw new ForbiddenOperationException("服务上架失败");
        }

    }
}
